{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "71144bf9",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/vector_stores/pinecone_metadata_filter.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "307804a3-c02b-4a57-ac0d-172c30ddc851",
   "metadata": {},
   "source": [
    "# Qdrant Vector Store - Default Qdrant Filters"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "3ceaf5c9",
   "metadata": {},
   "source": [
    "Example on how to use Filters from the qdrant_client SDK directly in your Retriever / Query Engine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4321e278",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-vector-stores-qdrant"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9466d0fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip3 install llama-index qdrant_client"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ad14111-0bbb-4c62-906d-6d6253e0cdee",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "import qdrant_client\n",
    "from IPython.display import Markdown, display\n",
    "from llama_index.core import VectorStoreIndex\n",
    "from llama_index.core import StorageContext\n",
    "from llama_index.vector_stores.qdrant import QdrantVectorStore\n",
    "from qdrant_client.http.models import Filter, FieldCondition, MatchValue\n",
    "\n",
    "client = qdrant_client.QdrantClient(location=\":memory:\")\n",
    "from llama_index.core.schema import TextNode\n",
    "\n",
    "nodes = [\n",
    "    TextNode(\n",
    "        text=\"りんごとは\",\n",
    "        metadata={\"author\": \"Tanaka\", \"fruit\": \"apple\", \"city\": \"Tokyo\"},\n",
    "    ),\n",
    "    TextNode(\n",
    "        text=\"Was ist Apfel?\",\n",
    "        metadata={\"author\": \"David\", \"fruit\": \"apple\", \"city\": \"Berlin\"},\n",
    "    ),\n",
    "    TextNode(\n",
    "        text=\"Orange like the sun\",\n",
    "        metadata={\"author\": \"Jane\", \"fruit\": \"orange\", \"city\": \"Hong Kong\"},\n",
    "    ),\n",
    "    TextNode(\n",
    "        text=\"Grape is...\",\n",
    "        metadata={\"author\": \"Jane\", \"fruit\": \"grape\", \"city\": \"Hong Kong\"},\n",
    "    ),\n",
    "    TextNode(\n",
    "        text=\"T-dot > G-dot\",\n",
    "        metadata={\"author\": \"George\", \"fruit\": \"grape\", \"city\": \"Toronto\"},\n",
    "    ),\n",
    "    TextNode(\n",
    "        text=\"6ix Watermelons\",\n",
    "        metadata={\n",
    "            \"author\": \"George\",\n",
    "            \"fruit\": \"watermelon\",\n",
    "            \"city\": \"Toronto\",\n",
    "        },\n",
    "    ),\n",
    "]\n",
    "\n",
    "openai.api_key = \"YOUR_API_KEY\"\n",
    "vector_store = QdrantVectorStore(\n",
    "    client=client, collection_name=\"fruit_collection\"\n",
    ")\n",
    "storage_context = StorageContext.from_defaults(vector_store=vector_store)\n",
    "index = VectorStoreIndex(nodes, storage_context=storage_context)\n",
    "\n",
    "\n",
    "# Use filters directly from qdrant_client python library\n",
    "# View python examples here for more info https://qdrant.tech/documentation/concepts/filtering/\n",
    "\n",
    "filters = Filter(\n",
    "    should=[\n",
    "        Filter(\n",
    "            must=[\n",
    "                FieldCondition(\n",
    "                    key=\"fruit\",\n",
    "                    match=MatchValue(value=\"apple\"),\n",
    "                ),\n",
    "                FieldCondition(\n",
    "                    key=\"city\",\n",
    "                    match=MatchValue(value=\"Tokyo\"),\n",
    "                ),\n",
    "            ]\n",
    "        ),\n",
    "        Filter(\n",
    "            must=[\n",
    "                FieldCondition(\n",
    "                    key=\"fruit\",\n",
    "                    match=MatchValue(value=\"grape\"),\n",
    "                ),\n",
    "                FieldCondition(\n",
    "                    key=\"city\",\n",
    "                    match=MatchValue(value=\"Toronto\"),\n",
    "                ),\n",
    "            ]\n",
    "        ),\n",
    "    ]\n",
    ")\n",
    "\n",
    "retriever = index.as_retriever(vector_store_kwargs={\"qdrant_filters\": filters})\n",
    "\n",
    "response = retriever.retrieve(\"Who makes grapes?\")\n",
    "for node in response:\n",
    "    print(\"node\", node.score)\n",
    "    print(\"node\", node.text)\n",
    "    print(\"node\", node.metadata)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
